package cn.imjzm.runcarserve.controller;

import cn.imjzm.runcarserve.pojo.User;
import cn.imjzm.runcarserve.service.RegistService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

import java.util.Map;
@CrossOrigin(origins = "*")
@Slf4j
@RestController
public class RegistController {

    @Autowired
    private RegistService registService;

    // 发送邮箱验证码
    @PostMapping("/send-verification-code")
    public Map<String, Object> sendVerificationCode(@RequestParam String email) {
        try {
            registService.sendEmailVerificationCode(email);
            return Map.of("status", "success", "message", "验证码已发送至邮箱");
        } catch (RuntimeException e) {
            // 处理频率限制等业务异常
            log.warn("发送验证码被限制 - 邮箱: {}, 原因: {}", email, e.getMessage());
            return Map.of("status", "error", "message", e.getMessage());
        } catch (Exception e) {
            log.error("发送验证码失败 - 邮箱: " + email, e);
            return Map.of("status", "error", "message", "发送验证码失败，请稍后重试");
        }
    }

    // 重置密码
    @PostMapping("/reset")
    public Map<String, Object> reset(@RequestParam String email,
                                          @RequestParam String code,
                                          @RequestParam(required = false) String password) {

        // 获取当前毫秒时间戳
        long currentTimeMillis = System.currentTimeMillis();

        // 将毫秒时间戳转换为 LocalDateTime
        LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis), ZoneOffset.UTC);

        try {
            String isValid = registService.reset(email, code,password);



                if("no1".equals(isValid)) {
                    return Map.of("success", false, "message", "验证码不存在");
                } else if("no2".equals(isValid)) {
                return Map.of("success", false, "message", "验证码已过期");
            } else if("noexist".equals(isValid)) {
                    return Map.of("success", false, "message", "邮箱不存在");
                }

                else if("success".equals(isValid)) {
                    return Map.of("success", true, "message", "修改成功");
                } else {
                    return Map.of("success", false, "message", "未知错误");
                }

        } catch (Exception e) {
            log.error("修改错误 - 邮箱: " + email, e);
            return Map.of("success", false, "message", "修改失败，请稍后重试");
        }
    }

    // 注册
    @PostMapping("/verify-code")
    public Map<String, Object> verifyCode(@RequestParam String email,
                                          @RequestParam String code,
                                          @RequestParam(required = false) String password) {

        // 获取当前毫秒时间戳
        long currentTimeMillis = System.currentTimeMillis();

        // 将毫秒时间戳转换为 LocalDateTime
        LocalDateTime createTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(currentTimeMillis), ZoneOffset.UTC);

        try {
            boolean isValid = registService.verifyEmailCode(email, code);
            if (isValid) {
                User user = new User();
                user.setUsername(email);
                user.setEmail(email);
                user.setPassword(password);
                user.setCreateTime(createTime);
                user.setStatus((short) 1);
                String result = registService.RegistUser(user);
                if("User already using".equals(result)) {
                    return Map.of("success", false, "message", "用户已存在");
                } else if("success".equals(result)) {
                    return Map.of("success", true, "message", "注册成功");
                } else {
                    return Map.of("success", false, "message", "未知错误");
                }
            } else {
                return Map.of("success", false, "message", "验证码无效或已过期");
            }
        } catch (Exception e) {
            log.error("注册失败 - 邮箱: " + email, e);
            return Map.of("success", false, "message", "注册失败，请稍后重试");
        }
    }
}
